package com.dongdongshop.controller;

import com.dongdongshop.common.AjaxResult;
import com.dongdongshop.common.ResultEnum;
import com.dongdongshop.pojo.TbContentCategory;
import com.dongdongshop.service.ContentCategoryService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("contentCategory")
public class ContentCategoryController {
    Logger logger = LoggerFactory.getLogger(ContentCategoryController.class);
    @DubboReference
    private ContentCategoryService contentCategoryService;

    /*广告类型管理*/
    /*跳转广告类型管理页面*/
    @RequestMapping("getContentCategoryHtml")
    public String getContentCategoryHtml(){
        return "/admin/content_category";
    }

    /*查询全部广告类型*/
    @RequestMapping("getContentCategoryAll")
    @ResponseBody
    public List<TbContentCategory> getContentCategoryAll(){
        return contentCategoryService.getContentCategoryAll();
    }

    /*添加广告类型*/
    @RequestMapping("addContentCategory")
    @ResponseBody
    public AjaxResult addContentCategory(TbContentCategory contentCategory){
        try {
            int i  = contentCategoryService.addContentCategory(contentCategory);
            if (i>0){
                return AjaxResult.R(ResultEnum.SUCCESS);
            }
            return AjaxResult.R(ResultEnum.ERROR);
        }catch (Exception e){
            logger.error("添加广告类型失败!错误详情:{}",e);
            return AjaxResult.R(ResultEnum.ERROR);
        }
    }
    /*修改广告类型*/
    /*1:回显*/
    @RequestMapping("updateContentCategoryEcho")
    @ResponseBody
    public AjaxResult updateContentCategoryEcho(Long id){
        try {
            TbContentCategory contentCategory  = contentCategoryService.addContentCategoryEcho(id);
            if (contentCategory != null){
                return AjaxResult.R(ResultEnum.SUCCESS).setData(contentCategory);
            }
            return AjaxResult.R(ResultEnum.ERROR);
        }catch (Exception e){
            logger.error("修改回显广告类型失败!错误详情:{}",e);
            return AjaxResult.R(ResultEnum.ERROR);
        }
    }
    /*2:修改*/
    @RequestMapping("updateContentCategory")
    @ResponseBody
    public AjaxResult updateContentCategory(TbContentCategory contentCategory){
        try {
            int i = contentCategoryService.updateContentCategory(contentCategory);
            if (i>0){
                return AjaxResult.R(ResultEnum.SUCCESS);
            }
            return AjaxResult.R(ResultEnum.ERROR);
        }catch (Exception e){
            logger.error("执行修改广告类型失败!错误详情:{}",e);
            return AjaxResult.R(ResultEnum.ERROR);
        }
    }
    /*删除广告类型*/
    @RequestMapping("deleteContentCategory")
    @ResponseBody
    public AjaxResult deleteContentCategory(Long id){
        try {
            int i = contentCategoryService.deleteContentCategory(id);
            if (i == 1){
                return AjaxResult.R(ResultEnum.SUCCESS);
            }else if (i==2){
                System.out.println("该分类下有内容,请先删除!");
                return AjaxResult.R(ResultEnum.ERROR);
            }
            return AjaxResult.R(ResultEnum.ERROR);
        }catch (Exception e){
            logger.error("执行删除广告类型失败!错误详情:{}",e);
            return AjaxResult.R(ResultEnum.ERROR);
        }
    }
}
